<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../layui/layui.js"></script>
    <script src="../js/jquery-1.8.0.min.js"></script>
    <script src="../js/ajax.js"></script>
    <link rel="stylesheet" href="../layui/css/layui.css">
    <link rel="stylesheet" href="../css/shousuokuan.css">
</head>

<body>
    <script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-sm  layui-btn-danger" lay-filter="del" lay-event="del">删除</a>
</script>






    <div class="layuimini-container">
        <div class="layuimini-main">
            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">

                    <form id="form" class="layui-form" action="">
                        <div class="layui-form-item">
                            <div class="layui-form-item">
                                <label class="layui-form-label">部门</label>
                                <div class="layui-inline">
                                    <select name="departmentId" id="departmentId" lay-search="">
                                        <option value=""></option>
                                    </select>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">操作人员</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="operatorName" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">客户名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="customName" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                            </div>
                            <label class="layui-form-label">起始时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="startDate" id="startDate" placeholder="请选择起始时间"
                                    autocomplete="off" class="layui-input">
                            </div>
                            <label class="layui-form-label">最后时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="endDate" id="endDate" placeholder="请选择最后时间" autocomplete="off"
                                    class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>

                        </div>
                    </form>

                </div>
            </fieldset>
        </div>
    </div>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn" lay-submit lay-filter="export">导出Excel</button>
        </div>
    </script>
    <table id="demo" lay-filter="test"></table>
    <script src="../js/host.js"></script>
    <script>


        $.ajaxSetup({
            headers: {
                "token": localStorage.getItem("token")
            }
        });

        layui.use(["table", 'form'], function () {
            var table = layui.table;
            var form = layui.form;
            var laydate = layui.laydate;
            //日期
            laydate.render({
                elem: '#startDate'
            });
            laydate.render({
                elem: '#endDate'
            });
            table.render({
                elem: '#demo'
                , url: host + '/SaleGoods' //数据接口
                , page: true //开启分页
                ,where: {departmentId:0}
                , headers: {
                    "token": localStorage.getItem("token")
                }
                , toolbar: '#toolbarDemo'
                , loading: true
                , cols: [[ //表头
                    { type: 'numbers', title: '序号', fixed: 'left' }
                    , { field: 'departmentName', title: '部门' }
                    , { field: 'staffName', title: '操作人' }
                    , { field: 'customName', title: '客户名称' }
                    , { field: 'commodityName', title: '商品名称' }
                    , { field: 'number', title: '销售数量' }
                    , { field: 'price', title: '销售单价' }
                    , { field: 'totalPrice', title: '销售金额' }
                    , { field: 'currDate', title: '销售时间' }
                    , { field: 'profit', title: '毛利润' }
                    , { field: 'interest_rate', title: '毛利率' ,templet: function(d){
                        return (d.interest_rate*100).toFixed(2)+"%"
                    }}
                ]]
            });

            new ajax(host + "/getDepartment", "get", {}, "json", function (data) {
                $.each(data, function (index, item) {
                    $("#departmentId").append("<option value='" + item.id + "'>" + item.departmentName + "</option>")
                })
                form.render("select")
            });
            


            form.on('submit(formDemo)', function (data) {
                //当前容器的全部表单字段，名值对形式：{name: value}】
                table.reload('demo', {
                    where: data.field
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    } //设定异步数据接口的额外参数
                    //,height: 300
                });
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });


            form.on('submit(export)', function (data) {
                //当前容器的全部表单字段，名值对形式：{name: value}】
                window.open(host + '/saleGoodsExport?' + $("#form").serialize());
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });


        })
    </script>
</body>

</html>